
package com.be.beadmin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.service.dto.input.SerialNumberItemDto;
import com.be.beadmin.service.dto.input.SerialNumberItemQueryCriteria;
import com.be.beadmin.service.dto.output.SerialNumberItemEx;
import com.be.beadmin.service.entity.SerialNumber;
import com.be.beadmin.service.entity.SerialNumberItem;
import com.be.beadmin.service.entity.SerialNumberItem_;
import com.be.beadmin.service.entity.SerialNumber_;
import com.be.beadmin.service.mapper.SerialNumberItemMapper;
import com.be.beadmin.service.mapper.SerialNumberItemMapperEx;
import com.be.beadmin.service.repository.SerialNumberItemRepository;
import com.be.beadmin.service.repository.SerialNumberRepository;
import com.be.beadmin.service.service.SerialNumberItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author chenwei
* @date 2023-01-16
**/
@Service
@RequiredArgsConstructor
public class SerialNumberItemServiceImpl implements SerialNumberItemService {

    @Autowired
    private  SerialNumberItemRepository serialNumberItemRepository;

    @Autowired
    private  SerialNumberItemMapper serialNumberItemMapper;
    @Autowired
    private  SerialNumberItemMapperEx serialNumberItemExMapper;

    @Autowired
    private SerialNumberRepository serialNumberRepository;

    @Override
    public Page<SerialNumberItemEx> getSerialNumberItemByPage(SerialNumberItemQueryCriteria serialNumberItemQueryCriteria) {
            Pageable pageable = PageRequest.of(
                    serialNumberItemQueryCriteria.getCurrent() -1,
                    serialNumberItemQueryCriteria.getPageSize());
            Page<SerialNumberItem> page = serialNumberItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, serialNumberItemQueryCriteria.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(SerialNumberItem_.delete), 0); //过滤为0
                    list.add(pre2);
                    if (!StringUtils.isEmpty(serialNumberItemQueryCriteria.getSerialNumberId())) {
                        Predicate pre3 = criteriaBuilder.equal(root.get(SerialNumberItem_.serialNumber)
                                .get(SerialNumber_.id), serialNumberItemQueryCriteria.getSerialNumberId()); //过滤为0
                        list.add(pre3);
                    }
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(serialNumberItemQueryCriteria.getSorters() != null && serialNumberItemQueryCriteria.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, serialNumberItemQueryCriteria.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(SerialNumberItem_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<SerialNumberItemEx> page1 = page.map(serialNumberItemExMapper::toEntity);
            return page1;

    }

    @Override
    public void addSerialNumberItem(SerialNumberItemDto serialNumberItemDto) {
            SerialNumber serialNumber = serialNumberRepository.findSerialNumberById(serialNumberItemDto.getSerialNumberId());
             if(ObjectUtil.isEmpty(serialNumber)) {
                 throw new BeadminException("请选择一个编码规则,添加相应的规则");
             }
             SerialNumberItem serialNumberItem = serialNumberItemMapper.toEntity(serialNumberItemDto);
             serialNumberItem.setSerialNumber(serialNumber);
             serialNumberItemRepository.save(serialNumberItem);

    }

    @Override
    public void updateSerialNumberItem(SerialNumberItemDto serialNumberItemDto) {
            SerialNumberItem serialNumberItem = serialNumberItemRepository.findSerialNumberItemById(serialNumberItemDto.getId());
            serialNumberItem = serialNumberItemMapper.toEntity(serialNumberItemDto);
            serialNumberItemRepository.save(serialNumberItem);
    }


    @Override
    public void delSerialNumberItem(String id) {
        SerialNumberItem serialNumberItem = serialNumberItemRepository.findSerialNumberItemById(id);
        serialNumberItem.setDelete(1);
        serialNumberItemRepository.save(serialNumberItem);
    }

    @Override
    public void delSerialNumberItems (List<String> ids) { }
}